package com.uuah.server.config.builders;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.uuah.config.i18n.CoreMessages;
import com.uuah.server.api.IUuahContext;
import com.uuah.server.api.config.IConfigurationBuilder;
import com.uuah.server.api.lifecycle.ILifecycleManager;
import com.uuah.server.config.ConfigResource;
import com.uuah.server.config.i18n.ServerMessages;
import com.uuah.server.exception.config.ConfigurationException;
import com.uuah.utils.StringUtils;

/**
 * A support class for {@link com.uuah.server.api.config.IConfigurationBuilder}
 * implementations that handles the logic of creating config arrays and
 * {@link java.util.Properties} arguments
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 上午09:52:08
 * @version 1.0.0
 */
public abstract class AbstractConfigurationBuilder implements
		IConfigurationBuilder {
	protected transient final Logger logger = LoggerFactory
			.getLogger(getClass());

	private boolean configured = false;
	protected ConfigResource[] configResources;

	public AbstractConfigurationBuilder(String configResources)
			throws ConfigurationException {
		this.configResources = loadConfigResource(StringUtils.splitAndTrim(
				configResources, ",; "));
	}

	public AbstractConfigurationBuilder(ConfigResource[] configResources) {
		this.configResources = configResources;
	}

	public void configure(IUuahContext uuahContext) throws ConfigurationException {
		if (configResources == null) {
			throw new ConfigurationException(CoreMessages
					.objectIsNull("Configuration Resource"));
		}
		try {
			doConfigure(uuahContext);
			applyLifecycle(uuahContext.getLifecycleManager());
			configured = true;
		} catch (Exception e) {
			logger.error(ServerMessages.configurationBuilderError(this)
					.getMessage(), e);
			throw new ConfigurationException(e);
		}
		if (logger.isInfoEnabled()) {
			logger.info(ServerMessages.configurationBuilderSuccess(this,
					createConfigResourcesString()).getMessage());
		}
	}

	protected abstract void doConfigure(IUuahContext uuahContext)
			throws Exception;

	protected abstract void applyLifecycle(ILifecycleManager lifecycleManager)
			throws Exception;

	public boolean isConfigured() {
		return configured;
	}

	protected ConfigResource[] loadConfigResource(String[] configs)
			throws ConfigurationException {
		try {
			configResources = new ConfigResource[configs.length];
			for (int i = 0; i < configs.length; i++) {
				configResources[i] = new ConfigResource(configs[i]);
			}
			return configResources;
		} catch (IOException e) {
			throw new ConfigurationException(e);
		}
	}

	protected String createConfigResourcesString() {
		StringBuffer configResourcesString = new StringBuffer();
		configResourcesString.append("[");
		for (int i = 0; i < configResources.length; i++) {
			configResourcesString.append(configResources[i]);
			if (i < configResources.length - 1) {
				configResourcesString.append(", ");
			}
		}
		configResourcesString.append("]");
		return configResourcesString.toString();
	}
}
